﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using Tesla.Business.BusinessLogic;
using Tesla.Service.DataContracts;
using Tesla.Util.Utilities;
using System.ServiceModel;
using System.Data.SqlClient;
using Tesla.Service.ServiceContracts;
using Tesla.Business.BusinessLogic.Implementations;
using Tesla.Business.BusinessLogic.Interfaces;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Tesla.Service.ServiceImplementations
{
    public class SvcPhase : ISvcPhase
    {


        public IList<BaseDC> DisplayAll(int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> baseDCs = null;
            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                baseDCs = blBase.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");
                if (rethrow)
                    throw;
            }
            return baseDCs;
        }

        public IList<BaseDC> SearchByName(string name, int gridSize, ref int? totalRecordCount)
        {
            IList<BaseDC> baseDCs = null;
            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                baseDCs = blBase.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");
                if (rethrow)
                    throw;
            }
            return baseDCs;
        }

        public IList<BaseDC> Read(int id)
        {
            IList<BaseDC> baseDCs = null;
            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                baseDCs = blBase.Read(id);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");
                if (rethrow)
                    throw;
            }
            return baseDCs;
        }

        public IList<BaseDC> ReadAllProjects()
        {
            IList<BaseDC> baseDCs = null;
            try
            {
                using (ContextBL cBL = new ContextBL())
                {
                    IPhaseBL iblPhase = cBL.CreateSpecializedBL(EntityMapper.PHASEBL) as IPhaseBL;
                    baseDCs = iblPhase.ReadAllProjects();
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");
                if (rethrow)
                    throw;
            }
            return baseDCs;
        }


        public int? Create(BaseDC bdc)
        {
            int? success = 0;

            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                success = blBase.Create(bdc);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");

                if (rethrow)
                    throw;
            }
            return success;
        }

        public int Update(BaseDC bdc)
        {
            int success = 0;

            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                success = blBase.Update(bdc);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");


                if (rethrow)
                    throw;
            }
            return success;
        }

        public IList<BaseDC> ReadByRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            IList<BaseDC> baseDCs = null;
            try
            {
                BaseOperationalBL blBase = BLFactory.CreateInstance(EntityMapper.PHASEBL);
                baseDCs = blBase.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "GADN Custom Exception Policy");
                if (rethrow)
                    throw;
            }
            return baseDCs;
        }
    }
}

